package cn.terminus.biz.customer.custominfo;

import cn.terminus.biz.customer.common.AbstractTerminusManager;
import cn.terminus.common.customer.bean.analysis.CustomerAnalysisDto;
import cn.terminus.common.customer.bean.analysis.CustomerStageNumResultDto;
import cn.terminus.common.customer.bean.analysis.CustomerNatureIndustryNumResultDto;
import cn.terminus.common.customer.bean.analysis.CustomerStageDto;
import cn.terminus.common.customer.bean.custominfo.dto.*;
import cn.terminus.data.customer.custominfo.dao.CustomInfoDao;
import cn.terminus.result.common.exception.DataAccessException;
import cn.terminus.result.common.page.TerminusPageAble;
import cn.terminus.result.common.page.TerminusPageInfo;
import cn.terminus.result.common.util.BeanCopyUtils;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * (CustomInfo)表服务实现类
 *
 * @author makejava
 * @since 2021-07-08 11:18:49
 */
@Component
public class CustomInfoManager extends AbstractTerminusManager {
    @Autowired
    private CustomInfoDao customInfoDao;

    public Integer add(CustomInfoNewDto dto) throws DataAccessException {
        try {
            return customInfoDao.add(dto);
        } catch (Exception e) {
            logger.error("CustomInfoManager新增失败，原因：", e);
            throw new DataAccessException(e);
        }
    }


    public Integer update(CustomInfoNewDto dto) throws DataAccessException {
        try {
            return customInfoDao.update(dto);
        } catch (Exception e) {
            logger.error("CustomInfoManager更新失败，原因：", e);
            throw new DataAccessException(e);
        }
    }

    public CustomInfoResultDto getDetail(CustomInfoGetsDto dto) throws DataAccessException {
        try {
            return customInfoDao.getDetail(dto);
        } catch (Exception e) {
            logger.error("CustomInfoManager新增失败，原因：", e);
            throw new DataAccessException(e);
        }
    }

    public Integer delete(CustomInfoDeleteDto dto) throws DataAccessException {
        try {
            return customInfoDao.delete(dto);
        } catch (Exception e) {
            logger.error("CustomInfoManager删除失败，原因：", e);
            throw new DataAccessException(e);
        }
    }

    public TerminusPageAble<CustomInfoResultDto> page(CustomInfoQueryDto dto, TerminusPageInfo page) throws DataAccessException {
        try {
            PageHelper.startPage(page.getCurrentPage(), page.getPageSize());
            Page<CustomInfoResultDto> result = (Page<CustomInfoResultDto>) customInfoDao.query(dto);
            return new TerminusPageAble<>(BeanCopyUtils.copy(result.getResult(), CustomInfoResultDto.class), page, result.getTotal());
        } catch (Exception e) {
            logger.error("CustomInfoManager分页查询失败，原因：", e);
            throw new DataAccessException(e);
        }
    }

    public Integer belongTo(CustomBelongtoDto customBelongtoDto) throws DataAccessException {
        try {
            return customInfoDao.belongTo(customBelongtoDto);
        } catch (Exception e) {
            logger.error("CustomInfoManager分配归属失败，原因：", e);
            throw new DataAccessException(e);
        }

    }

    public List<GetCustomInfoByNameResultDto> getNameListByName(GetCustomInfoByNameDto dto) throws DataAccessException {
        try {
            return customInfoDao.getNameListByName(dto);
        } catch (Exception e) {
            logger.error("CustomInfoManager根据名称获取客户详情失败，原因：", e);
            throw new DataAccessException(e);
        }
    }

    public List<CustomInfoQueryNameResultDto> queryByIds(List<Long> customerIdList) throws DataAccessException {
        try {
            return customInfoDao.queryByIds(customerIdList);
        } catch (Exception e) {
            logger.error("根据id查询用户名称列表，原因：", e);
            throw new DataAccessException(e);
        }
    }

    public GetCustomInfoByNameResultDto getDetailById(Long customerId) throws DataAccessException {
        try {
            return customInfoDao.getDetailById(customerId);
        } catch (Exception e) {
            logger.error("根据id查询用户名称列表，原因：", e);
            throw new DataAccessException(e);
        }
    }

    public List<ChannelCustomerNumberResultDto> getChannelCustomerNumber(CustomerChannelUserDto dto) throws DataAccessException {
        try {
            return customInfoDao.getChannelCustomerNumber(dto);
        } catch (Exception e) {
            logger.error("获取客户数失败，原因：", e);
            throw new DataAccessException(e);
        }
    }

    public Integer queryByCustomName(CustomInfoGetsDto dto) throws DataAccessException {
        try {
            return customInfoDao.queryByCustomName(dto);
        } catch (Exception e) {
            logger.error("根据客户名查客户失败，原因：", e);
            throw new DataAccessException(e);
        }
    }

    public List<CustomerStageNumResultDto> analysisCustomerStage(CustomerAnalysisDto dto) throws DataAccessException {
        try {
            return customInfoDao.analysisCustomerStage(dto);
        } catch (Exception e) {
            logger.error("统计数据失败，原因：", e);
            throw new DataAccessException(e);
        }
    }


    public List<CustomerNatureIndustryNumResultDto> analysisNature(CustomerStageDto dto) throws DataAccessException {
        try {
            return customInfoDao.analysisNature(dto);
        } catch (Exception e) {
            logger.error("统计数据失败，原因：", e);
            throw new DataAccessException(e);
        }
    }

    public List<CustomerNatureIndustryNumResultDto> analysisIndustry(CustomerStageDto dto) throws DataAccessException {
        try {
            return customInfoDao.analysisIndustry(dto);
        } catch (Exception e) {
            logger.error("统计数据失败，原因：", e);
            throw new DataAccessException(e);
        }
    }

    public List<CustomInfoBaseDto> getBaseCustomerInfoByIds(List<Long> ids) throws DataAccessException {
        try {
            return customInfoDao.getBaseCustomerInfoByIds(ids);
        } catch (Exception e) {
            logger.error("统计数据失败，原因：", e);
            throw new DataAccessException(e);
        }
    }
    public void changeCustomerChannel(CustomerChannelUserDto dto) throws DataAccessException {
        try {
            customInfoDao.changeCustomerChannel(dto);
        } catch (Exception e) {
            logger.error("置空失败，原因：", e);
            throw new DataAccessException(e);
        }
    }
}
